home *** CD-ROM | disk | FTP | other *** search
/ Atari Mega Archive 1 / Atari Mega Archive - Volume 1.iso / lists / mint / l_0799 / 583 / execgem.c next >
Encoding:
C/C++ Source or Header  |  1994-08-27  |  3.2 KB  |  133 lines

  1. /* execgem -- exec `old' GEM (exec_os) or a gem.sys (-DINITPRG)
  2.    make sure we're on console, zero its pgroup to reduce GEM SIGTTIN/OU
  3.    problems (don't ask), exec.  INITPRG can also be a script so you
  4.    can create an account `gem' in /etc/passwd if you want with this
  5.    as loginshell and put setup things in the script. (last line execs
  6.    the real gem.)
  7.  
  8.    BUGS: still no way to leave GEM & free all its memory etc. other
  9.    than reboot. :-(  if someone manages to shut down GEM and later
  10.    restart it without spectacular crashes and losing memory _please_
  11.    tell us how...
  12. */
  13.  
  14. #include <stdio.h>
  15. #include <string.h>
  16. #include <stat.h>
  17. #include <fcntl.h>
  18. #include <ioctl.h>
  19. #include <unistd.h>
  20. #include <errno.h>
  21. #include <mintbind.h>
  22. #include <basepage.h>
  23. #include <st-out.h>
  24.  
  25. #ifdef INITPRG
  26. #define init_prg INITPRG
  27. #define init_tail "\0"
  28. #define init_env 0L
  29. #else
  30. #define init_prg 0L
  31. #define init_tail ""
  32. #define init_env 0L
  33. #endif
  34.  
  35. #define EXEC_OS 0x4feL
  36.  
  37. /* define how to call functions with stack parameter passing */
  38. #ifdef __TURBOC__
  39. #define ARGS_ON_STACK cdecl
  40. #else
  41. #define ARGS_ON_STACK
  42. #endif
  43.  
  44. #ifndef P_
  45. # ifdef __STDC__
  46. #  define P_(x) x
  47. # else
  48. #  define P_(x) ()
  49. # endif
  50. #endif
  51.  
  52. /* not yet in st-out.h... */
  53. #ifndef F_PROT_P
  54. #define F_PROT_P    0x00        /* no read or write */
  55. #define F_PROT_G    0x10        /* any access OK */
  56. #define F_PROT_S    0x20        /* any super access OK */
  57. #define F_PROT_PR    0x30        /* any read OK, no write */
  58. #define F_PROT_I    0x40        /* invalid page */
  59. #endif
  60.  
  61. int
  62. main(argc, argv)
  63.     int argc;
  64.     char **argv;
  65. {
  66.     long stack;
  67.     extern int __mint;
  68.  
  69.     if (__mint) {
  70.         struct stat sb, st;
  71.         BASEPAGE *bp;
  72.         long pgrp, r;
  73.         char tmp[0x100];
  74.  
  75.         if (Fcntl(-1, &pgrp, TIOCGPGRP) ||
  76.             Fcntl(-1, &sb, FSTAT) ||
  77.             (Fxattr(0, "u:\\dev\\vt00", &st) &&
  78.             Fxattr(0, "u:\\dev\\console", &st)) ||
  79.             st.st_dev != sb.st_dev ||
  80.             st.st_ino != sb.st_ino) {
  81.  
  82.             Cconws ("Hey GEM is not X! :)  console only...\r\n");
  83.             return 1;
  84.         }
  85.  
  86.         /* just to be sure... */
  87.         (void) Fclose (0);
  88.         (void) Fcntl(-1, 0L, F_DUPFD);
  89.         (void) Fclose (1);
  90.         (void) Fcntl(-1, 0L, F_DUPFD);
  91.         (void) Fclose (2);
  92.         (void) Fcntl(-1, 0L, F_DUPFD);
  93.  
  94.         /* `turn off' job control on /dev/console, GEM seems to have
  95.            problems with it...  apparently even multitos :-(  */
  96.         r = 0;
  97.         (void) Fcntl(0, &r, TIOCSPGRP);
  98.  
  99.         stack = Super (0L);
  100.         if (init_prg) {
  101.             /* not Pexec so init_prg gets ARGV and can be a script.
  102.             */
  103.             execl (init_prg, init_prg, (char *)0);
  104.             if (errno == ENOEXEC || errno == ENOENT)
  105.                 execl ("/bin/sh", "/bin/sh", init_prg, (char *)0);
  106.             r = errno;
  107.         } else {
  108.             bp = (BASEPAGE *)Pexec(7,
  109.               (char *)((long)F_FASTLOAD | F_ALTLOAD | F_ALTALLOC | F_PROT_S),
  110.               (char *)"\0", init_env);
  111.             bp->p_tbase = *((char **) EXEC_OS );
  112.             r = -Pexec(206, (char *)"GEM", bp, 0L);
  113.         }
  114.         Super (stack);
  115.         sprintf (tmp, "exec %s failed: %s (%d)\r\n",
  116.             init_prg ? init_prg : "GEM", strerror (r), (int) r);
  117.         (void) Fcntl(0, &pgrp, TIOCSPGRP);
  118.         Cconws (tmp);
  119.         return r;
  120.     } else {
  121.         register void ARGS_ON_STACK (*f) P_((long));
  122.  
  123.         /* no MiNT -> just jump thru exec_os... */
  124.         stack = Super (0L);
  125.         f = (void ARGS_ON_STACK (*) P_((long))) *((long *)EXEC_OS);
  126.         Super (stack);
  127.  
  128.         (*f)((long) _base);
  129.         /*NOTREACHED*/
  130.         return 0;
  131.     }
  132. }
  133.